#下载图片
import re
import requests
from lxml import etree
 
def download(html):
    #通过正则匹配
    # pic_url = re.findall('"objURL":"(.*?)",',html, re.S)
    # pic_url = re.findall('\/Portals\/0\/C_Info\/ImageFile\/(.*?),',html, re.S)
    etree_html = etree.HTML(html)
    # c_html = etree_html.xpath('//*[@id="txt"]/text()')
    c_html = etree_html.xpath('//*[@id="txt"]/text()')
    # print(etree.tostring(c_html))
    print(c_html)
    pic_url = re.findall('\/Portals\/0\/C_Info\/ImageFile\/(.*?),',c_html, re.S)


    print(pic_url)
    i = 1
    for key in pic_url:
        print("开始下载图片："+key +"\r\n")
        try:
            pic = requests.get("http://www.toptour.cn"+key, timeout=10)
        except requests.exceptions.ConnectionError:
            print('图片无法下载')
            continue
        #保存图片路径
        dir = '马陵山：春山觅花海读书煮春茶' + str(i) + '.jpg'
        fp = open(dir, 'wb')
        fp.write(pic.content)
        fp.close()
        i += 1
def main():
        # url = 'https://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&sf=1&fmq=&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&fm=index&pos=history&word=lay'
        # url = 'http://www.toptour.cn/tabid/204/InfoID/274499/frtid/1649/Default.aspx'
        url = 'http://www.toptour.cn/tabid/204/InfoID/274427/frtid/1649/Default.aspx?&__U=1585559209661#curinfoid=274427'
        result = requests.get(url)
        print(result.text)
        download(result.text)
 
 
if __name__ == '__main__':
        main()